var ScrambledEggs;
(function (ScrambledEggs) {
    var Game = (function () {
        function Game() {
            this._modules = [];
            this._empty = new ScrambledEggs.Modules.Empty();
            var key = ScrambledEggs.Utility.QueryString.Find("key");
            if(key == null) {
                key = ScrambledEggs.Utility.Key.generate();
            }
            this.GameState = new ScrambledEggs.GameStateProvider(key);
        }
        Game.prototype.run = function () {
            var me = this;
            var ready = false;
            me.GameState.load(function () {
                ScrambledEggs.Cooldowns.AutoResolve(me.GameState);
                if(ready) {
                    me.onUpdate(me._empty);
                } else {
                    ready = true;
                }
            });
            me.fetchModules();
            me.initializeModules();
            if(ready) {
                me.onUpdate(me._empty);
            } else {
                ready = true;
            }
            (window).withGame = function (callback) {
                callback(me, me.GameState.State);
                me.onUpdate(me._empty);
            };
        };
        Game.prototype.fetchModules = function () {
            this._modules.push(new ScrambledEggs.Modules.Birth());
            this._modules.push(new ScrambledEggs.Modules.Meta());
            this._modules.push(new ScrambledEggs.Modules.Message());
            this._modules.push(new ScrambledEggs.Modules.FirstMoves());
            this._modules.push(new ScrambledEggs.Modules.Play());
            this._modules.push(new ScrambledEggs.Modules.Inventory());
            this._modules.push(new ScrambledEggs.Modules.BabyFinale());
            this._modules.push(new ScrambledEggs.Modules.YoungAdult());
            this._modules.push(new ScrambledEggs.Modules.HatEvent());
            this._modules.push(new ScrambledEggs.Modules.Job());
            this._modules.push(new ScrambledEggs.Modules.EndGame());
            this._modules.push(new ScrambledEggs.Modules.StatusDisplay());
        };
        Game.prototype.initializeModules = function () {
            var me = this;
            $.each(this._modules, function (i, mod) {
                mod.initialize(me.GameState, function () {
                    me.onUpdate(mod);
                });
            });
        };
        Game.prototype.onUpdate = function (source) {
            var me = this;
            $.each(me._modules, function () {
                var m = this;
                m.update(me.GameState, source);
            });
        };
        return Game;
    })();
    ScrambledEggs.Game = Game;    
    $(document).ready(function () {
        var game = new Game();
        game.run();
        $(".noselect").on("selectstart", function (event) {
            event.preventDefault();
            event.stopPropagation();
            return false;
        });
    });
})(ScrambledEggs || (ScrambledEggs = {}));
